import axios from "axios";
// const urlParams = new URLSearchParams(window.location.href.split("?")[1]);
// const agentParams = {};
// urlParams.forEach((value, key) => {
//   agentParams[key] = value;
// });
// let list = agentParams.agentUrl.split("/");

// let agentUrl = list[0] + "//" + list[2];

// const hostname = (window.location.hostname);
// if(hostname=='127.0.0.1'){
  let agentUrl = 'http://agent.ccic-test.com.cn/'
// }

const service = axios.create({
  baseURL: agentUrl,
  // baseURL: '',
  timeout: 300 * 1000,
});

// 请求拦截
service.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截
service.interceptors.response.use((res) => {
  const { status, data, msg } = res;
  if (status === 200) {
    return data;
  } else {
    return Promise.reject(msg);
  }
});

// post 请求
export const post = (url, params, headers) => {
  return new Promise((resolve, reject) => {
    service
      .post(
        url,
        params,
        (headers = {
          "Content-Type": "application/json",
          Connection: "keep-alive",
        })
      )
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

// get 请求
export const get = (url, params, headers) => {
  return new Promise((resolve, reject) => {
    service
      .get(url, {
        params: params,
        headers: {
          "Content-Type": "application/json",
        },
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
